Imports System.Configuration
Imports System.Web.Configuration
Imports System.Web

Public Class MakeModelService
    Private Shared _provider As VehicleProvider = Nothing
    Private Shared _providers As VehicleCollection = Nothing
    Private Shared _lock As Object = New Object
    Private Shared isInitialized As Boolean = False
    Private Shared initializationException As Exception

    Public ReadOnly Property Provider() As VehicleProvider
        Get
            Return _provider
        End Get
    End Property

    Public ReadOnly Property Providers() As VehicleCollection
        Get
            Return _providers
        End Get
    End Property

    Public Shared Function GetAllMakes() As System.Data.DataTable
        LoadProviders()
        Return _provider.GetAllMakes
    End Function

    Public Shared Function GetModels(ByVal MakeId As String) As System.Data.DataTable
        LoadProviders()
        Return _provider.GetModels(MakeId)
    End Function

    Public Shared Function GetModelCategories() As System.Data.DataTable
        LoadProviders()
        Return _provider.GetModelCategories
    End Function

    Public Shared Function GetModels(ByVal MakeId As String, ByVal ModelCategoryId As String) As System.Data.DataTable
        LoadProviders()
        Return _provider.GetModels(MakeId, ModelCategoryId)
    End Function

    Public Shared Function CreateMake(ByVal MakeDescription As String, ByVal LoggedOnUsername As String) As Boolean
        LoadProviders()
        Return _provider.CreateMake(MakeDescription, LoggedOnUsername)
    End Function

    Public Shared Function UpdateMake(ByVal MakeId As String, ByVal NewMakeDescription As String, ByVal LoggedOnUsername As String) As Boolean
        LoadProviders()
        Return _provider.UpdateMake(MakeId, NewMakeDescription, LoggedOnUsername)
    End Function

    Public Shared Function DeleteMake(ByVal MakeId As String, ByVal LoggedOnUsername As String) As Boolean
        LoadProviders()
        Return _provider.DeleteMake(MakeId, LoggedOnUsername)
    End Function

    Public Shared Function CreateModel(ByVal MakeId As String, ByVal ModelDescription As String, ByVal ModelCategoryId As String, ByVal LoggedOnUser As String) As Boolean
        LoadProviders()
        Return _provider.CreateModel(MakeId, ModelDescription, ModelCategoryId, LoggedOnUser)
    End Function

    Public Shared Function UpdateModel(ByVal ModelId As String, ByVal MakeId As String, ByVal ModelDescription As String, ByVal MOdelCategoryId As String, ByVal LoggedOnUser As String) As Boolean
        LoadProviders()
        Return _provider.UpdateModel(ModelId, MakeId, ModelDescription, MOdelCategoryId, LoggedOnUser)
    End Function

    Public Shared Function DeleteModel(ByVal ModelId As String, ByVal LoggedOnUsername As String) As Boolean
        LoadProviders()
        Return _provider.DeleteModel(ModelId, LoggedOnUsername)
    End Function

    Public Shared Function CreateModelCategory(ByVal CategoryShortDescription As String, ByVal CategoryLongDescription As String, ByVal LoggedOnUser As String) As Boolean
        LoadProviders()
        Return _provider.CreateModelCategory(CategoryShortDescription, CategoryLongDescription, LoggedOnUser)
    End Function

    Public Shared Function UpdateModelCategory(ByVal ModelCategoryId As String, ByVal CategoryShortDescription As String, ByVal CategoryLongDescription As String, ByVal LoggedOnUser As String) As Boolean
        LoadProviders()
        Return _provider.UpdateModelCategory(ModelCategoryId, CategoryShortDescription, CategoryLongDescription, LoggedOnUser)
    End Function

    Public Shared Function DeleteModelCategory(ByVal ModelCategoryId As String, ByVal LoggedOnUsername As String) As Boolean
        LoadProviders()
        Return _provider.DeleteModelCategory(ModelCategoryId, LoggedOnUsername)
    End Function

    Private Shared Sub LoadProviders()
        Try
            'Get the feature's configuration info
            Dim qc As VehicleConfiguration = CType(ConfigurationManager.GetSection("MakeModelProvider"), VehicleConfiguration)
            If ((qc.DefaultProvider Is Nothing) _
                        OrElse ((qc.Providers Is Nothing) _
                        OrElse (qc.Providers.Count < 1))) Then
                Throw New System.Configuration.Provider.ProviderException(("You must specify a valid default provider."))
            End If
            _providers = New VehicleCollection
            ProvidersHelper.InstantiateProviders(qc.Providers, _providers, GetType(VehicleProvider))
            _providers.SetReadOnly()
            _provider = _providers(qc.DefaultProvider)
            If (_provider Is Nothing) Then
                Throw New ConfigurationErrorsException("You must specify a default provider for the feature.", qc.ElementInformation.Properties("defaultProvider").Source, qc.ElementInformation.Properties("defaultProvider").LineNumber)
            End If
        Catch ex As Exception
            initializationException = ex
            isInitialized = True
            Throw ex
        End Try
        isInitialized = True
        'error-free initialization
    End Sub
End Class
